package com.zhanglijie.improve.collections.copyonwrite;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/12/1 0001 23:23
 */
public class Demo {
    private static  int a;
    private static int b;
    static CopyOnWriteArrayList cp = new CopyOnWriteArrayList();

    public static void main(String[] args) throws  Exception {
        cp.add("first");
        int i = 0;
        for(;;){
            i++;
            Thread t1 = new Thread(() -> {
                a = 1;
                //cp.set(0,"bb");
            });

            Thread t2 = new Thread(() -> {

                    mysleep(1000);

              //  cp.get(0);
                b = a;
            });

            t1.start();
            t2.start();
            mysleep(2000000);

            if(b != 1){
                System.out.println(a+" ==第"+i+"次=="+b);
                break;
            }
        }
    }

    private static void mysleep(int timeout) {
        long start = System.nanoTime();
        long end;
        do{
            end = System.nanoTime();
        }while(end-start<=timeout);
    }
}
